﻿using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace VietThinks.Web.Mvc
{
    public static class UrlHelperExtensions
    {
        public static string Action<TController>(this UrlHelper urlHelper, Expression<Func<TController, ActionResult>> expression)
            where TController : Controller
        {
            return urlHelper.RouteUrl(ControllerExtensions.CreateRoutes(expression));
        }

        public static string ActionEx<TController>(this UrlHelper urlHelper, Expression<Func<TController, ActionResult>> expression, bool requiredSsl)
            where TController : Controller
        {
            var requestUrl = HttpContext.Current.Request;

            if(!requestUrl.IsLocal && requiredSsl)
            {
                return string.Format("{0}{1}{2}", "https://", HttpContext.Current.Request.Url.Authority,
                                        urlHelper.RouteUrl(ControllerExtensions.CreateRoutes(expression)));
            }

            return urlHelper.RouteUrl(ControllerExtensions.CreateRoutes(expression));
        }

        public static string ActionEx<TController>(this UrlHelper urlHelper, Expression<Func<TController, ActionResult>> expression)
            where TController : Controller
        {
            return ActionEx(urlHelper, expression, false);
        }
    }
}
